<?php

namespace App\Listeners\TopicCreated;

use App\Events\TopicCreated;
use App\Models\Score;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class AssignScoreToUser
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  TopicCreated  $event
     * @return void
     */
    public function handle(TopicCreated $event)
    {
        $topic = $event->topic;
        $info = ['original' => $topic->toArray()];
        $info['action_id'] = $topic->id;
        $info['action_title'] = $topic->titleExcerpt;
        $info['target_id'] =  '';
        $info['target_title'] =  '';
        $info['target_username'] =  '';
        Score::assignForCreateTopic($topic->uid, null, null, $info);

        logger(sprintf(
            "%s, assign score to user: %s, info: %s",
            __METHOD__, $topic->uid, json_encode($info, 336)
        ));
    }
}
